package com.qfedu.config;

import com.qfedu.factory.MyAdaptableJobFactory;
import com.qfedu.schedule.MyJob;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @author LiJH
 * @date 2023-02-01
 */
@Configuration
public class QuarzConfig {

    @Bean
    //注入job
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(MyJob.class);
        return jobDetailFactoryBean;
    }

    @Bean
    //注入触发器
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        JobDetail object = jobDetailFactoryBean.getObject();
        cronTriggerFactoryBean.setJobDetail(object); //绑定任务
        cronTriggerFactoryBean.setCronExpression("0/2 * * * * ?"); //cron表达式
        return cronTriggerFactoryBean;
    }
    @Bean
    //注入调度者
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, MyAdaptableJobFactory myAdaptableJobFactory){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());
        //设置job工厂 ，目的是将job注入到IOC容器中
        schedulerFactoryBean.setJobFactory(myAdaptableJobFactory);
        return schedulerFactoryBean;
    }
}